package org.apache.http.message;

import org.apache.http.FormattedHeader;
import org.apache.http.Header;
import org.apache.http.RequestLine;
import org.apache.http.StatusLine;
import org.apache.http.a.a;
import org.apache.http.a.d;
import org.apache.http.annotation.Immutable;
import org.apache.http.e;

@Immutable
public class f implements LineFormatter {
    @Deprecated
    public static final f a = new f();
    public static final f b = new f();

    protected int a(e eVar) {
        return eVar.a().length() + 4;
    }

    protected d a(d dVar) {
        if (dVar == null) {
            return new d(64);
        }
        dVar.a();
        return dVar;
    }

    protected void a(d dVar, Header header) {
        String name = header.getName();
        String value = header.getValue();
        int length = name.length() + 2;
        if (value != null) {
            length += value.length();
        }
        dVar.a(length);
        dVar.a(name);
        dVar.a(": ");
        if (value != null) {
            dVar.a(value);
        }
    }

    protected void a(d dVar, RequestLine requestLine) {
        String method = requestLine.getMethod();
        String uri = requestLine.getUri();
        dVar.a((((method.length() + 1) + uri.length()) + 1) + a(requestLine.getProtocolVersion()));
        dVar.a(method);
        dVar.a(' ');
        dVar.a(uri);
        dVar.a(' ');
        appendProtocolVersion(dVar, requestLine.getProtocolVersion());
    }

    protected void a(d dVar, StatusLine statusLine) {
        int a = ((a(statusLine.getProtocolVersion()) + 1) + 3) + 1;
        String reasonPhrase = statusLine.getReasonPhrase();
        if (reasonPhrase != null) {
            a += reasonPhrase.length();
        }
        dVar.a(a);
        appendProtocolVersion(dVar, statusLine.getProtocolVersion());
        dVar.a(' ');
        dVar.a(Integer.toString(statusLine.getStatusCode()));
        dVar.a(' ');
        if (reasonPhrase != null) {
            dVar.a(reasonPhrase);
        }
    }

    public d appendProtocolVersion(d dVar, e eVar) {
        a.a((Object) eVar, "Protocol version");
        int a = a(eVar);
        if (dVar == null) {
            dVar = new d(a);
        } else {
            dVar.a(a);
        }
        dVar.a(eVar.a());
        dVar.a('/');
        dVar.a(Integer.toString(eVar.b()));
        dVar.a('.');
        dVar.a(Integer.toString(eVar.c()));
        return dVar;
    }

    public d formatHeader(d dVar, Header header) {
        a.a((Object) header, "Header");
        if (header instanceof FormattedHeader) {
            return ((FormattedHeader) header).getBuffer();
        }
        d a = a(dVar);
        a(a, header);
        return a;
    }

    public d formatRequestLine(d dVar, RequestLine requestLine) {
        a.a((Object) requestLine, "Request line");
        d a = a(dVar);
        a(a, requestLine);
        return a;
    }

    public d formatStatusLine(d dVar, StatusLine statusLine) {
        a.a((Object) statusLine, "Status line");
        d a = a(dVar);
        a(a, statusLine);
        return a;
    }
}
